<?php
namespace Home\Controller;
//use Think\Controller;
class UserController extends BaseController {
	 public function login(){
   if(IS_POST){
   	$username=$_POST["username"];
     $password=$_POST["password"];
     $m=M("member");
	   $user=$m->field("id,username,nickname")->where("username='{$username}' and password='{$password}'")->find();
   if($user){
   	//查询用户权限
     session_start();
	   $_SESSION["username"]=$username;
	 $user["code"]=0;
	 $user["rules"]=$this->GetRules($user["id"]);
	 $this->ajaxReturn($user);
//	 $this->redirect("/home/user/info");
     }else {
     	$obj=(object)array();
		$obj->code=101;
		$obj->message="查询失败！";
// 	 echo "用户名或密码错误";
 $this->ajaxReturn($obj);
   }
}else if(IS_GET){
	 $this->display();
	
}
   }
	
	private function GetRules($uid){
		$access=M("auth_group_access");
		$group=$access->field("rules")->join("think_auth_group on think_auth_group.id=think_auth_group_access.group_id")->where("uid=".$uid)->find();
		$rule=M("auth_rule");
		 $userRules=$rule->where("id in (".$group["rules"].") and is_menu=1")->select();
		 return $userRules;
	}
	   
	  public function index(){
	  	$username=$_SESSION["username"];
		$m=M("member");
		$result=$m->where("username='$username'")->find();
		$this->assign('result',$result);
	  	if(IS_POST){
   	 	  if($_POST["user"]=="exit"){
		session_destroy();
			  header("Location:http://127.0.0.1:8060/home/user/login");
	}
   	 }else if(IS_GET){
   	 	$this->display();
   	 }
	  } 
	   
	   
	public function auth(){
		 if(IS_POST){
   	 	  if($_POST["user"]=="exit"){
		session_destroy();
			  header("Location:http://127.0.0.1:8060/home/user/login");
	}
   	 }
	if($_SESSION["username"]){
    	$username=$_SESSION["username"];
    	 $m=M("member");
	 $res=$m->where("username='{$username}'")->select();
	  $id=$res[0]["id"];
	 $ret=$m->find($id);
	 $this->assign('ret',$ret);
	 if(count($res)>0){
//	 	echo $ret;
$this->display();
	}
//	}else{
//		$this->display();
//	}
	}
	}
	public function info(){
		 $m=M("member");
	$username=$_SESSION["username"];
    if(IS_GET){
	 $res=$m->where("username='{$username}'")->select();
	  $id=$res[0]["id"];
	 $ret=$m->find($id);
	 $this->assign('ret',$ret);
	if(count($res)>0){
//	 	echo $ret;
$this->display();
	}
	}
if(IS_POST){
	 if($_POST["user"]=="exit"){
		session_destroy();
			  header("Location:http://127.0.0.1:8060/home/user/login");
	}
	if($m->create()) {
$result = $m->where("username='{$username}'")->save();
	$obj=(object)array();
	$obj->code=0;
	$obj->message="修改成功";
if($result) {
$this->ajaxReturn($obj);
}
}else{
$obj=(object)array();
	$obj->code=101;
	$obj->message="修改失败";
$this->ajaxReturn($obj);
}
}
}
	
	public function items(){
		 if(IS_POST){
   	 	  if($_POST["user"]=="exit"){
		session_destroy();
			  header("Location:http://127.0.0.1:8060/home/user/login");
	}
	}
		if($_SESSION["username"]){
    	$username=$_SESSION["username"];
    	 $m=M("member");
	 $res=$m->where("username='{$username}'")->select();
	  $id=$res[0]["id"];
	 $ret=$m->find($id);
	 $this->assign('ret',$ret);
	 if(count($res)>0){
//	 	echo $ret;
$this->display();
	}
//	}else{
//		$this->display();
	}
	}
//	}
	public function role(){
		 if(IS_POST){
   	 	  if($_POST["user"]=="exit"){
		session_destroy();
			  header("Location:http://127.0.0.1:8060/home/user/login");
	}
   	 }
		if($_SESSION["username"]){
    	$username=$_SESSION["username"];
    	 $m=M("member");
	 $res=$m->where("username='{$username}'")->select();
	  $id=$res[0]["id"];
	 $ret=$m->find($id);
	 $this->assign('ret',$ret);
	 if(count($res)>0){
//	 	echo $ret;
$this->display();
	}
	}else{
		$this->display();
	}
	}

	public function updatePwd(){
	 $m=M("member");
	$username=$_SESSION["username"];
    if(IS_GET){
	 $res=$m->where("username='{$username}'")->select();
	  $id=$res[0]["id"];
	 $ret=$m->find($id);
	 $this->assign('ret',$ret);
	if(count($res)>0){
//	 	echo $ret;
$this->display();
	}
	}
if(IS_POST){
	 if($_POST["user"]=="exit"){
		session_destroy();
			  header("Location:http://127.0.0.1:8060/home/user/login");
	}
	if($m->create()) {
$result = $m->where("username='{$username}'")->save();
	$obj=(object)array();
	$obj->code=0;
	$obj->message="修改成功";
if($result) {
$this->ajaxReturn($obj);
}
}else{
$obj=(object)array();
	$obj->code=101;
	$obj->message="修改失败";
$this->ajaxReturn($obj);
}
}
	}

//查询数据库
public function serverdb(){
	$columnindex=$_GET['order'][0]['column'];
			$dir=$_GET['order'][0]['dir'];
			$start=$_GET['start'];
			$length=$_GET['length'];
			$search=$_GET['search']['value'];
	$m = M('member');
	if($columnindex==1){
		$column="username";
	}else if($columnindex==2){
		$column="nickname";
	}else if($columnindex==3){
		$column="address";
	}else if($columnindex==4){
		$column="phone";
	}
	$res=$m->where("username like '%{$search}%' or nickname like '%{$search}%' or address like '%{$search}%' or phone like  '%{$search}%'")->field('username,nickname,address,phone')->order($column,$dir)->limit($start,$length)->select();
		$obj=(object)array();
			$ret->data=array();
			$ret->draw=$_GET['draw'];	
			$ret->search=$search;
		for ($i=0; $i <count($res) ; $i++) { 
		$ret->data[]=array($i+1,$res[$i]["username"],$res[$i]["nickname"],$res[$i]["address"],$res[$i]["phone"],"<button type='button' class='btn btn-default btn-sm'>查看</button><button type='button' class='btn btn-default btn-sm'>删除</button><button type='button' class='btn btn-default btn-sm'>编辑</button>");
		}
		$sum=count($m->select());
		$num=count($res);
			$ret->recordsFiltered=$num;
			$ret->recordsTotal=$sum;
			$this->ajaxReturn($ret);
}

    public function serverdblist(){
	$columnindex=$_GET['order'][0]['column'];
			$dir=$_GET['order'][0]['dir'];
			$start=$_GET['start'];
			$length=$_GET['length'];
			$search=$_GET['search']['value'];
	$m = M('member');
	if($columnindex==1){
		$column="username";
	}else if($columnindex==2){
		$column="nickname";
	}else if($columnindex==3){
		$column="address";
	}else if($columnindex==4){
		$column="phone";
	}
	$res=$m->where("username like '%{$search}%' or nickname like '%{$search}%' or address like '%{$search}%' or phone like  '%{$search}%' or work like  '%{$search}%' or role like  '%{$search}%' or birthday like  '%{$search}%'")->field('username,nickname,address,phone,work,role,birthday')->order($column,$dir)->limit($start,$length)->select();
		$obj=(object)array();
			$ret->data=array();
			$ret->draw=$_GET['draw'];	
			$ret->search=$search;
		for ($i=0; $i <count($res) ; $i++) { 
		$ret->data[]=array($i+1,$res[$i]["username"],$res[$i]["nickname"],$res[$i]["address"],$res[$i]["phone"],$res[$i]["work"],$res[$i]["role"],$res[$i]["birthday"],"<button type='button' class='btn btn-default btn-sm '  data-toggle='modal' data-target='#modal-update' name='edit1'>编辑</button><button type='button' class='btn btn-default btn-sm'   data-toggle='modal' data-target='#modal-del' name='del1'>删除</button>");
		}
		$sum=count($m->select());
		$num=count($res);
			$ret->recordsFiltered=$num;
			$ret->recordsTotal=$sum;
			$this->ajaxReturn($ret);
}
 public function bigger(){
		 $m=M("member");
		 $res=$m->field("MAX(id)  as id ")->find();
			$this->ajaxReturn($res);
      }
  public function tianjia(){
  	     $username=$_POST['username'];
       $Form = D('member');
	   $user=$Form->field("username")->where("username='{$username}'")->find();
	   if($user){
    $this->success('该用户已存在！');
	   }else{
	   	if($Form->create()) {
$result = $Form->add();
//if($result) {
//$this->success('数据添加成功！');
//}else{
//$this->error('数据添加错误！');
//}
}
	   }
    $auth_grou_aces= D('auth_group_access');
	   if($auth_grou_aces->create()){
$result =$auth_grou_aces->add();
	   	if($result) {
$this->success('数据添加成功！');
}else{
$this->error('数据添加错误！');
}
	   }
	   
}  
  
    
   public function loadrole(){
	 	$m=M("auth_group");
   	     $res=$m->field("title")->select();
			$this->ajaxReturn($res);
   }
 	 public function genxin(){
 	 	if(IS_POST){
 	 		$username=$_REQUEST["username-edit"];
$Form = M('member');
if($Form->create()) {
$result = $Form->where("username='{$username}'")->save();
if($result) {
$this->success('操作成功！');
}else{
$this->error('写入错误！');
}
}else{
$this->error($Form->getError());
}
}
 	 	}
	 
	 public function delete(){
	 	 	if(IS_PSOT){
			$Form=M("member");
				$username=$_REQUEST["username-del"];
				if($username=="刘一"){
					$this->success('该用户不可删！');
				}else{
			$result=$Form->where("username='{$username}'")->delete();
					if($result) {
$this->success('数据删除成功！');
}else{
$this->error('数据删除错误！');
}
				}
}	
			}
	 
	 //从数据库获取用户身份
	 public function rolelist(){
	 		$columnindex=$_GET['order'][0]['column'];
			$dir=$_GET['order'][0]['dir'];
			$start=$_GET['start'];
			$length=$_GET['length'];
			$search=$_GET['search']['value'];
			
	 	$m=M("auth_group");
		if($columnindex==1){
		$column="title";
	};
		$res=$m->where("title like '%{$search}%' or id like '%{$search}%'")->field('title,id')->order($column,$dir)->limit($start,$length)->select();
		$obj=(object)array();
			$ret->data=array();
			$ret->draw=$_GET['draw'];	
			$ret->search=$search;
		for ($i=0; $i <count($res) ; $i++) { 
		$ret->data[]=array($i+1,$res[$i]["title"],"是","<button type='button'  class='btn btn-default btn-sm'   style='display:none'></button><button type='button' class='btn btn-default btn-sm'    data-toggle='modal' data-target='#modal-del' id='roledel'  data-id='".$res[$i]['id']."'> 删除</button>");
		}
		$sum=count($m->select());
		$num=count($res);
			$ret->recordsFiltered=$num;
			$ret->recordsTotal=$sum;
			$this->ajaxReturn($ret);
	 }
	 
	 
	  public function authlist(){
	 		$columnindex=$_GET['order'][0]['column'];
			$dir=$_GET['order'][0]['dir'];
			$start=$_GET['start'];
			$length=$_GET['length'];
			$search=$_GET['search']['value'];
	 	$m=M("auth_group");
		if($columnindex==1){
		$column="title";
	};
		$res=$m->where("title like '%{$search}%' or id like '%{$search}%'")->field('title,id')->order($column,$dir)->limit($start,$length)->select();
		$obj=(object)array();
			$ret->data=array();
			$ret->draw=$_GET['draw'];	
			$ret->search=$search;
		for ($i=0; $i <count($res) ; $i++) { 
		$ret->data[]=array($i+1,$res[$i]["title"],"是","<button type='button'  class='btn btn-default btn-sm'   style='display:none'></button><button type='button' class='btn btn-default btn-sm'    data-toggle='modal' data-target='#role-default-edit'  id='roledel'  data-id='".$res[$i]['id']."'>添加权限</button>");
		}
		$sum=count($m->select());
		$num=count($res);
			$ret->recordsFiltered=$num;
			$ret->recordsTotal=$sum;
			$this->ajaxReturn($ret);
	 }
	 
	 
	 //查询普通用户
 	
	  public function newrole(){
	  	$Form = D('auth_group');
	  	$title=$_POST["title"];
		  $res=$Form->where("title='{$title}'")->select();
		  if($res){
$this->success('该角色已存在，请勿重复添加！');
		  }else{
if($Form->create()) {
$result = $Form->add();
if($result) {
$this->success('角色添加成功！');
}else{
$this->error('数据添加错误！');
}
}else{
$this->error($Form->getError());
}
		  }
} 
 public function search( ){
   	         $id=$_POST["id"];
   	 			$columnindex=$_GET['order'][0]['column'];
			$dir=$_GET['order'][0]['dir'];
			$start=$_GET['start'];
			$length=$_GET['length'];
			$search=$_GET['search']['value'];
	 	$access=M("auth_group_access");
		$uid=$access->field("uid")->where("group_id=3")->select();
		  $m=M("member");
		  $res=$m->field("username,role")->join("think_auth_group_access on think_auth_group_access.uid=think_member.id ")->where("group_id=".$id)->select();
		  $obj=(object)array();
			$ret->data=array();
			$ret->draw=$_GET['draw'];	
			$ret->search=$search;
		for ($i=0; $i <count($res) ; $i++) { 
		$ret->data[]=array($i+1,$res[$i]["username"],$res[$i]["role"]);
		}
		$sum=count($m->select());
		$num=count($res);
			$ret->recordsFiltered=$num;
			$ret->recordsTotal=$sum;
			$this->ajaxReturn($res);
 	 		}
   public function role_delete(){
   	if(IS_PSOT){
   		  $title=$_POST["username-del"];
			$Form=D("auth_group");
			$res=$Form->field("title")->where("title='{$title}'")->select();
			if($res[0]["title"]=="超级管理员"||$res[0]["title"]=="管理员"||$res[0]["title"]=="普通用户"){
			$this->success(" 该角色不可删");	
			}else{
			$result=$Form->where("title='{$title}'")->delete();
if($result) {
$this->success('角色删除成功！');
}else{
$this->error('数据删除错误！');
}
			}
}	
   }
   //权限详细信息
   public function roledetail(){
   	//身份id
   	      $id=$_POST["id"];
		  $access=M("auth_group");
		$group=$access->field("rules")->where("id=".$id)->find();
		$rule=M("auth_rule");
		 $userRules=$rule->where("id in (".$group["rules"].")")->select();
			$this->ajaxReturn($userRules);
   }
  
   public function rules_delete(){
   //删除id
   	$id=$_POST["rule-id"];
	//级别id
	$master_id=$_POST["master-id"];
	$m=M("auth_group");
   }
}
